﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapstoneProjectAEReader.Entities.Models;
using iTextSharp.text.pdf;
using System.Threading;
using GhostscriptSharp;
using GhostscriptSharp.Settings;
using System.Drawing;

namespace PdfReaderControlLibrary
{
    public class EbookObjectGenerator
    {
        public Ebook GenerateEbookObj(string filePath)
        {
            Ebook myEbook = new Ebook();
            //File type
            FileInfo temp = new FileInfo(filePath);
            if (temp.Extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase))
            {
                myEbook.Type = ".pdf";
            }
            PdfReader metaReader = new PdfReader(filePath);
            myEbook.PageCount = metaReader.NumberOfPages;
            myEbook.Title = Path.GetFileNameWithoutExtension(filePath);
            if (!metaReader.Info.ContainsKey("Author"))
            {
                myEbook.Author = "";
            }
            else
            {
                myEbook.Author = metaReader.Info["Author"].Trim();
                if (String.IsNullOrWhiteSpace(myEbook.Author))
                {
                    myEbook.Author = "N/A";
                }
            }
            myEbook.Path = filePath;
            myEbook.ImportDate = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            myEbook.Rating = 0;
            myEbook.Publisher = "";
            myEbook.CurrentPage = 1;
            metaReader.Close();

            myEbook.Group = new List<string>();
            myEbook.BookmarkPath = "AeReaderData\\Temp\\" + myEbook.Title + ".xml";
            //TODO later

            return myEbook;
        }


        private static void GetPdfThumbnail(string sourcePdfFilePath, string destinationPngFilePath)
        {
            // Use GhostscriptSharp to convert the pdf to a png
            GhostscriptWrapper.GenerateOutput(sourcePdfFilePath, destinationPngFilePath,
                new GhostscriptSettings
                {
                    Device = GhostscriptDevices.pngalpha,
                    Page = new GhostscriptPages
                    {
                        // Only make a thumbnail of the first page
                        Start = 1,
                        End = 1,
                        AllPages = false
                    },
                    Resolution = new Size
                    {
                        // Render at 72x72 dpi
                        Height = 72,
                        Width = 72
                    },
                    Size = new GhostscriptPageSize
                    {
                        // The dimentions of the incoming PDF must be
                        // specified. The example PDF is US Letter sized.
                        Native = GhostscriptPageSizes.letter
                    }
                }
            );
        }
    }
}
